using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ipi.MCC.Pages
{
    public class MCCListSettings : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        protected EncodedLiteral elPageTitle;
        protected EncodedLiteral elDescription;
        protected Button btnActivteMCC;
        protected Button btnDeactivateMCC;

        protected SPList lstCurrent;

        protected InputFormSection ifsMCCColumn;
        protected InputFormControl ifcDeActivate;



        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.EnsureUpdatePanelFixups();  
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string sListID = Request["List"];

            lstCurrent = this.Web.Lists[new Guid(sListID)];

            SetControls();

            if (lstCurrent.Fields.ContainsField(MCC.Global.Functions.TreePosition_InternalColumnName))
            {
                btnActivteMCC.Visible = false;
                btnDeactivateMCC.Visible = true;
            }
            else
            {
                btnActivteMCC.Visible = true;
                btnDeactivateMCC.Visible = false;
            }
        
        }

        private void EnsureUpdatePanelFixups()
        {
            if (this.Page.Form != null)
            {
                string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
                if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                {
                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
                }
            }

            ScriptManager.RegisterStartupScript(this, typeof(Microsoft.SharePoint.WebControls.LayoutsPageBase), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
        }


        protected void OnClickDeActivate(Object Sender, EventArgs e)
        {
            
            Button btnDeActivate = (Button)Sender;
            SPWeb webRoot = this.Site.RootWeb;


            if (btnDeActivate.CommandName == "Activate")
            {

                SPField fld = webRoot.Fields.GetFieldByInternalName(MCC.Global.Functions.TreePosition_InternalColumnName);
                lstCurrent.Fields.Add(fld);
                lstCurrent.Update();

                btnActivteMCC.Visible = false;
                btnDeactivateMCC.Visible = true;

            }
            else 
            {
                lstCurrent.Fields.Delete(MCC.Global.Functions.TreePosition_InternalColumnName);
                lstCurrent.Update();

                btnActivteMCC.Visible = true;
                btnDeactivateMCC.Visible = false;
            }

        }

        private void SetControls()
        {
            elPageTitle.Text = Resources.Resources.ListPage_Title;
            elDescription.Text = Resources.Resources.ListPage_Description;

            ifsMCCColumn.Title = Resources.Resources.ListPage_ifsMCCColumn_Title;
            ifsMCCColumn.Description = Resources.Resources.ListPage_ifsMCCColumn_Description;

            ifcDeActivate.LabelText = Resources.Resources.ListPage_ifcDeActivate_LabelText;
            btnActivteMCC.Text = Resources.Resources.ListPage_Activate;
            btnDeactivateMCC.Text = Resources.Resources.ListPage_Deactivate;

        }

        protected void OnClickOK(Object Sender, EventArgs e)
        {            
            Response.Redirect(this.Site.MakeFullUrl(lstCurrent.DefaultView.ServerRelativeUrl));
        }
    }
}
